package com.gmrz.uap.fidohislog;

import com.gmrz.uap.cfg.SystemConst;
import com.gmrz.uap.common.AuthTypeInterceptor;
import com.gmrz.uap.common.ConditionConst;
import com.gmrz.uap.common.ControllerSupport;
import com.gmrz.uap.common.OperTypeInterceptor;
import com.gmrz.uap.fidolog.FidoLogService;
import com.gmrz.uap.model.FidoLog;
import com.gmrz.uap.resource.ResourceInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.UnsupportedEncodingException;

/**
 * 日志历史表的controller
 */
public class FidoHisLogController extends ControllerSupport {

    private static final Logger LOG = LogManager.getLogger(FidoHisLogController.class);

    /**
     * 配置的多个拦截器
     */
    @Before({ResourceInterceptor.class, OperTypeInterceptor.class, AuthTypeInterceptor.class})
    public void index() {
    }


    /**
     * 根据前台发过来的参数进行查询，可以进行 业务历史日志 分页，排序
     */
    public void list() {

        int page = getParaToInt(SystemConst.DATATABLE_PAGINATE_OFFSET, SystemConst.DATATABLE_PAGINATE_OFFSET_DEFAULT);
        int rows = getParaToInt(SystemConst.DATATABLE_PAGINATE_LIMIT, SystemConst.DATATABLE_PAGINATE_LIMIT_DEFAULT);

        String sort = getPara(SystemConst.DATATABLE_PAGINATE_SORT, SystemConst.DEFAULT_ORDERBY_CREATETS);
        String order = getPara(SystemConst.DATATABLE_PAGINATE_ORDER, SystemConst.DATATABLE_PAGINATE_ORDER_DEFAULT);

        String startTime = getPara(ConditionConst.CONDITION_START_TIME, null);
        String endTime = getPara(ConditionConst.CONDITION_END_TIME, null);

        FidoLog fidoLog = getModel(FidoLog.class);
        fidoLog.put(ConditionConst.CONDITION_START_TIME, startTime);//开始时间
        fidoLog.put(ConditionConst.CONDITION_END_TIME, endTime);//结束时间

        String tenantPara = getCookie("tenantId");
        String tenantId = getPara("tenantId", tenantPara);
        fidoLog.setTenantId(tenantId);

        LOG.debug("FIDO HISTORY Log request params : {}", fidoLog);

        // 业务历史日志 的分页查询
        Page<FidoLog> descPage = FidoHisLogService.me.findFidoHisLogListForPaginate(fidoLog, page, rows, sort, order, condition);

        result.put(SystemConst.DATATABLE_PAGINATE_TOTAL, descPage.getTotalRow());
        result.put(SystemConst.DATATABLE_PAGINATE_ROWS, descPage.getList());

        render(responseJsonRender(result));
    }

    public void info() {
        String flID = getPara("flID");
        String infoType = getPara("infoType");
        String result = null;
        try {
            result = FidoHisLogService.me.findFidoLogInfo(flID,infoType);
        } catch (UnsupportedEncodingException e) {
            LOG.error("get info error",e);
        }
        render(responseJsonRender(result));
    }

}
